package com.goldmantis.alitrip.service;

import com.goldmantis.alitrip.common.BaseService;
import com.goldmantis.alitrip.common.SysOrgTreeModel;
import com.goldmantis.alitrip.common.SysTreeModel;
import com.goldmantis.alitrip.mapper.SysOrgMapper;
import com.goldmantis.alitrip.pojo.SysOrg;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * @Author:Shmily
 * @Description:
 * @Date:Create in 9:46 2018/2/11
 */
@Service
public class SysOrgService extends BaseService<SysOrg> {

    @Autowired
    private SysOrgMapper sysOrgMapper;


    @Override
    public List<SysOrg> select(SysOrg t) {
        Example example = new Example(SysOrg.class);
        example.setOrderByClause("CREATETIME DESC ");
        Example.Criteria criteria = example.createCriteria();

        List<SysOrg> sysOrgs = this.sysOrgMapper.selectByExample(example);
        return sysOrgs;
    }

    public SysOrg getById(Long orgId) {
        return this.sysOrgMapper.getById(orgId);
    }


    public List<SysOrgTreeModel> getSysTreeModel() {

        List<SysOrgTreeModel> sysTreeModelList = new ArrayList<>();

        SysOrg sysOrgCondition=new SysOrg();
        //sysOrgCondition.setIsDelete((short) 0);
        List<SysOrg> sysOrgs = this.sysOrgMapper.select(sysOrgCondition);


        //获取根目录
        Iterator<SysOrg> iterator = sysOrgs.iterator();
        while (iterator.hasNext()){
            SysOrg next = iterator.next();
            if (next.getOrgsupid().longValue()==1) {
                SysOrgTreeModel sysOrgTreeModel = new SysOrgTreeModel();
                sysOrgTreeModel.setId(next.getOrgid());
                sysOrgTreeModel.setLabel(next.getOrgname());
                sysOrgTreeModel.setRootPath(next.getPath());
                sysTreeModelList.add(sysOrgTreeModel);
                iterator.remove();
            }
        }


        //递归获取子节点
        for (SysOrgTreeModel sysOrgModel : sysTreeModelList) {
            sysOrgModel.setChildren(getChild(sysOrgModel.getId(), sysOrgs));
        }

        return sysTreeModelList;
    }

    private List<SysOrgTreeModel> getChild(Long id, List<SysOrg> allOrgs) {

        //获取子菜单
        List<SysOrgTreeModel> sysTreeModelList = new ArrayList<>();

        Iterator<SysOrg> iterator = allOrgs.iterator();
        while (iterator.hasNext()){
            SysOrg next = iterator.next();
            if (next.getOrgsupid().longValue()==id.longValue()) {
                SysOrgTreeModel sysOrgTreeModel = new SysOrgTreeModel();
                sysOrgTreeModel.setId(next.getOrgid());
                sysOrgTreeModel.setLabel(next.getOrgname());
                sysOrgTreeModel.setRootPath(next.getPath());
                sysTreeModelList.add(sysOrgTreeModel);
                iterator.remove();
            }
        }

        //递归获取子节点
        for (SysOrgTreeModel sysOrgModel : sysTreeModelList) {
            sysOrgModel.setChildren(getChild(sysOrgModel.getId(), allOrgs));
        }

        if (sysTreeModelList.size() == 0) {
            return null;
        }
        return sysTreeModelList;

    }

}
